package omq.filetransfer;

import java.io.IOException;
import java.rmi.RemoteException;

import javax.xml.rpc.ServiceException;

import omq.exception.OMQPropertiesReaderException;
import omq.exception.OMQException;
import omq.api.logger.*;
import omq.utils.*;

/*
 * OMQ FileTransfer:
 * -----------------
 * 
 * OMQ FileTransfer system is a fully operational file system transfer protocol over OMQ.
 * It sends files to messages in OMQ queue and later receives them back to files.
 *  
 * All system parameters (including OMQ parameters and file-system parameters) are configured using these 2 properties file: 
 * 		for file sender   - omqsend.properties
 * 		for file receiver - omqreceive.properties
 * 
 * Following are the runnable options:
 *    
 * OMQSendFile - sends one specific file specified in parameter omq.ft.filename to a queue.
 * OMQSendFiles - a deamon that wait for new files on the folder omq.ft.path and sends all the files from this folder.
 * OMQReceiveFile - receives only one file from queue into the folder omq.ft.path. 
 * OMQReceiveFiles - a deamon that samples the queue every interval and saves the messages as files. 
 * there are 2 properties file to configure.
 *  
 *  Author  	  	: Hagai Izenberg
 *  Creating Date 	: 20-05-2008
 *  Company 		: OMQI
 */

public class OMQReceiveFile {

	/**
	 * @param args
	 * @throws ClassNotFoundException 
	 * @throws IOException 
	 * @throws OMQException 
	 */
	public static void main(String[] args)
	{

		final String defaultPropertiesFile = "omqreceive.properties";
		String propertiesFile = defaultPropertiesFile;
		boolean success=true;
		
		OMQLogger.getInstance().info("##########################################################################");
		OMQLogger.getInstance().info("# OMQ File Transfer - receive 1 file started at "+(FileUtil.time("yyyy.MM.dd 'at' hh:mm:ss")));		
		OMQLogger.getInstance().info("##########################################################################");

		if (args.length>0)
		{
			if (args[0].compareToIgnoreCase("-classpath")!= 0)
			{
				propertiesFile = args[0];
				OMQLogger.getInstance().info("using given properties file named "+propertiesFile);
			}
		}
		else
		{
			OMQLogger.getInstance().info("Using default properties file named "+propertiesFile+", you can pass a properties file name as a parameter to this java program");
		}

		OMQFileTransfer sender;
		try 
		{	
			OMQPropertiesReader prop = OMQPropertiesReader.createOMQPropertiesReader(propertiesFile);
			sender = new OMQFileTransfer(prop);
			success = sender.receiveFile();
		}
		catch (OMQFileTransferInitException e) 
		{
			// TODO Auto-generated catch block
			OMQLogger.getInstance().error("failed to init OMQ file transfer enviroment",e);
			
			success = false;
		}
		catch (OMQFileTransferException e) 
		{
			// TODO Auto-generated catch block
			OMQLogger.getInstance().error("failed to receive file",e);
			
			success = false;
		}		
		catch (OMQPropertiesReaderException e) 
		{
			// TODO Auto-generated catch block
			OMQLogger.getInstance().error("failed to read properties file",e);
			
			success = false;
		}
		catch (ServiceException e) 
		{
			// TODO Auto-generated catch block
			OMQLogger.getInstance().error("failed due to ServiceException",e);
			
			success = false;
		}
		if (success)
			OMQLogger.getInstance().info("OMQ File Transfer - receive file - ended succesfully");
		else
			OMQLogger.getInstance().info("OMQ File Transfer - receive file - ended unsuccesfully");

	}

}

